#include "Game.hpp"

using namespace TheEmpires;

Game::Game()
{
	mRenderer		= NULL;
}

Game::~Game()
{
	if(mRenderer)
	{
		delete mRenderer;
	}

	if(mSprites)
	{
		delete mSprites;
	}
}

void Game::SetWindow(int width, int height, std::string title)
{
	mWidth			= width;
	mHeight			= height;
	mTitle			= title;

	mWindow			= new RenderWindow(sf::VideoMode(mWidth, mHeight, 32), mTitle);

	mRenderer		= new Renderer(mWindow);
	mRenderer->SetWindow(mWidth, mHeight);

	mSprites		= new SpriteLoader();
	mSprites->LoadSprites("graphics/");
}

void Game::Loop()
{
	while(mWindow->IsOpened())
	{
		sf::Event event;

		while(mWindow->GetEvent(event))
		{
			if(event.Type == sf::Event::Closed)
			{
				mWindow->Close();
			}

			else if(event.Type == sf::Event::Resized)
			{
				mRenderer->SetWindow(event.Size.Width, event.Size.Height);
			}
		}

		if(mRenderer->StartFrame())
		{
			mRenderer->DrawRectangle(Color(0, 0, 255, 255), Vector2f(10, 10), Vector2f(50, 50), float(int(mClock.GetElapsedTime() * 1000) / 5 % 360));

			Sprite* sprite		= mSprites->GetSprite(1);
			mRenderer->DrawSprite(sprite, Vector2f(100, 10));

			mRenderer->EndFrame();
		}

		mWindow->Display();
	}
}
